<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form id="update-user-form" class="layui-form layuimini-form" lay-filter="update-user-form">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label layui-bg-blue" style="width:150px">旧密码：</label>
            <div class="layui-input-inline">
                <input type="password" id="editOldPassword" name="editOldPassword" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label layui-bg-blue" style="width:150px">新密码：</label>
            <div class="layui-input-inline">
                <input type="password" id="editNewPassword1" name="editNewPassword1" lay-verify="required" autocomplete="off" class="layui-input">
            </div>

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label layui-bg-blue"  style="width:150px">再次输入新密码：</label>
            <div class="layui-input-inline">
                <input type="password" id="editNewPassword2" name="editNewPassword2" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
         <div class="layui-inline">
             <div class="layui-input-inline">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
             </div>
         </div>
    </div>
</form>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
            console.log(param);//测试是否获取到表单数据，调试模式下在页面控制台查看

            let new1=$("#editNewPassword1").val().trim()
            let new2=$("#editNewPassword2").val().trim()

            if(new1!=new2){
                layer.msg('输入的两次新密码不一致！');
            }

            if(new1==new2) {
                // ajax方式更新用户
                $.ajax({
                    url: "/modifyPassword",
                    data: {"old_password":$("#editOldPassword").val().trim(),"new_password":$("#editNewPassword1").val().trim()},
                    success: function (data) {
                        if (data.status == 1) {
                            layer.close(layer.index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            layer.msg('密码修改成功');
                        } else {
                            layer.msg('原密码输入错误');
                        }
                    },
                    error: function () {
                        console.log("ajax error");
                    }
                });
            }
            // 阻止表单跳转
            return false;
        });
    });
</script>
</body>
</html>